

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://image.lxandzjy.top/2022/06/06/8871b9e332dbe.png">
  <link rel="icon" href="https://image.lxandzjy.top/2022/06/06/8871b9e332dbe.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="zjy">
  <meta name="keywords" content="学习笔记,vue,axios,,JavaScript,前端,后端">
  
    <meta name="description" content="Vue中使用svg图标遇到这个问题，是我在学习vue-admin-template 模板的时候学习到的。花裤衩大佬手摸手，带你优雅的使用 icon 文章地址https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903517564436493 手摸手，带你用vue撸后台系列   文章地址 https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903476661583880 使用 svg-sprite">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue中使用svg图标">
<meta property="og:url" content="https://zjy365.github.io/2021/10/20/vue%E4%B8%AD%E4%BD%BF%E7%94%A8svg%E5%9B%BE%E6%A0%87/index.html">
<meta property="og:site_name" content="土猪的个人博客">
<meta property="og:description" content="Vue中使用svg图标遇到这个问题，是我在学习vue-admin-template 模板的时候学习到的。花裤衩大佬手摸手，带你优雅的使用 icon 文章地址https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903517564436493 手摸手，带你用vue撸后台系列   文章地址 https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903476661583880 使用 svg-sprite">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-20T12:46:25.000Z">
<meta property="article:modified_time" content="2021-11-20T09:19:50.951Z">
<meta property="article:author" content="zjy">
<meta property="article:tag" content="vue">
<meta property="article:tag" content="svg">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Vue中使用svg图标 - 土猪的个人博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"zjy365.github.io","root":"/","version":"1.9.1","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>

  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>土猪</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('https://image.lxandzjy.top/2022/06/06/aea923ea1a0e8.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue中使用svg图标"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-10-20 20:46" pubdate>
          2021年10月20日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.8k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          32 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue中使用svg图标</h1>
            
            <div class="markdown-body">
              
              <h2 id="Vue中使用svg图标"><a href="#Vue中使用svg图标" class="headerlink" title="Vue中使用svg图标"></a>Vue中使用svg图标</h2><h3 id="遇到这个问题，是我在学习vue-admin-template-模板的时候学习到的。"><a href="#遇到这个问题，是我在学习vue-admin-template-模板的时候学习到的。" class="headerlink" title="遇到这个问题，是我在学习vue-admin-template 模板的时候学习到的。"></a>遇到这个问题，是我在学习vue-admin-template 模板的时候学习到的。</h3><h4 id="花裤衩大佬"><a href="#花裤衩大佬" class="headerlink" title="花裤衩大佬"></a>花裤衩大佬</h4><p>手摸手，带你优雅的使用 icon 文章地址<strong><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903517564436493">https://juejin.cn/post/6844903517564436493</a></strong></p>
<p>手摸手，带你用vue撸后台系列   文章地址 <strong><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903476661583880">https://juejin.cn/post/6844903476661583880</a></strong></p>
<h3 id="使用-svg-sprite"><a href="#使用-svg-sprite" class="headerlink" title="使用 svg-sprite"></a>使用 svg-sprite</h3><p>接下来我们就要自己来制作 <code>svg-sprite</code> 了。这里要使用到 <a href="https://link.juejin.cn/?target=https://github.com/kisenka/svg-sprite-loader">svg-sprite-loader</a> 这个神器了， 它是一个 webpack loader ，可以将多个 svg 打包成 <code>svg-sprite</code> 。</p>
<p>我们来介绍如何在 <code>vue-cli</code> 的基础上进行改造，加入 <code>svg-sprite-loader</code>。</p>
<p>我们发现<code>vue-cli</code>默认情况下会使用 <code>url-loader</code> 对svg进行处理，会将它放在<code>/img</code> 目录下，所以这时候我们引入<code>svg-sprite-loader</code> 会引发一些冲突。</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-regexp">//</span>默认`vue-cli` 对svg做的处理，正则匹配后缀名为.svg的文件，匹配成功之后使用 url-loader 进行处理。<br> &#123;<br>    test: <span class="hljs-regexp">/\.(png|jpe?g|gif|svg)(\?.*)?$/</span>,<br>    loader: <span class="hljs-string">&#x27;url-loader&#x27;</span>,<br>    options: &#123;<br>      limit: <span class="hljs-number">10000</span>,<br>      name: utils.assetsPath(<span class="hljs-string">&#x27;img/[name].[hash:7].[ext]&#x27;</span>)<br>    &#125;<br>&#125;<br>复制代码<br></code></pre></td></tr></table></figure>

<p>解决方案有两种，最简单的就是你可以将 test 的 svg 去掉，这样就不会对svg做处理了，当然这样做是很不友善的。</p>
<ul>
<li>你不能保证你所有的 svg 都是用来当做 icon的，有些真的可能只是用来当做图片资源的。</li>
<li>不能确保你使用的一些第三方类库会使用到 svg。</li>
</ul>
<p>所以最安全合理的做法是使用 webpack 的 <a href="https://link.juejin.cn/?target=https://webpack.js.org/configuration/module/%23rule-exclude">exclude</a> 和 <a href="https://link.juejin.cn/?target=https://webpack.js.org/configuration/module/%23rule-include">include</a> ，让<code>svg-sprite-loader</code>只处理你指定文件夹下面的 svg，<code>url-loaer</code>只处理除此文件夹之外的所以 svg，这样就完美解决了之前冲突的问题。 代码如下</p>
<h3 id="我觉得重要的是vue-config-js-对svg的处理（放前面）"><a href="#我觉得重要的是vue-config-js-对svg的处理（放前面）" class="headerlink" title="我觉得重要的是vue.config.js 对svg的处理（放前面）"></a>我觉得重要的是vue.config.js 对svg的处理（放前面）</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>  <span class="hljs-title function_">chainWebpack</span>(<span class="hljs-params">config</span>) &#123;<br>    config.<span class="hljs-title function_">plugin</span>(<span class="hljs-string">&#x27;preload&#x27;</span>).<span class="hljs-title function_">tap</span>(<span class="hljs-function">() =&gt;</span> [<br>      &#123;<br>        <span class="hljs-attr">rel</span>: <span class="hljs-string">&#x27;preload&#x27;</span>,<br>        <span class="hljs-attr">fileBlacklist</span>: [<span class="hljs-regexp">/\.map$/</span>, <span class="hljs-regexp">/hot-update\.js$/</span>, <span class="hljs-regexp">/runtime\..*\.js$/</span>],<br>        <span class="hljs-attr">include</span>: <span class="hljs-string">&#x27;initial&#x27;</span><br>      &#125;<br>    ])<br><br>    <span class="hljs-comment">// 当页面特别的多的时候会有很多不必要的请求</span><br>    config.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;prefetch&#x27;</span>)<br><br>    <span class="hljs-comment">// 设置svg-sprite-loader 处理svg这样就可以使用封装的svg组件</span><br>    config.<span class="hljs-property">module</span><br>      .<span class="hljs-title function_">rule</span>(<span class="hljs-string">&#x27;svg&#x27;</span>)<br>      .<span class="hljs-property">exclude</span>.<span class="hljs-title function_">add</span>(<span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;src/icons&#x27;</span>))<br>      .<span class="hljs-title function_">end</span>()<br>    config.<span class="hljs-property">module</span><br>      .<span class="hljs-title function_">rule</span>(<span class="hljs-string">&#x27;icons&#x27;</span>)<br>      .<span class="hljs-title function_">test</span>(<span class="hljs-regexp">/\.svg$/</span>)<br>      .<span class="hljs-property">include</span>.<span class="hljs-title function_">add</span>(<span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;src/icons&#x27;</span>))<br>      .<span class="hljs-title function_">end</span>()<br>      .<span class="hljs-title function_">use</span>(<span class="hljs-string">&#x27;svg-sprite-loader&#x27;</span>)<br>      .<span class="hljs-title function_">loader</span>(<span class="hljs-string">&#x27;svg-sprite-loader&#x27;</span>)<br>      .<span class="hljs-title function_">options</span>(&#123;<br>        <span class="hljs-attr">symbolId</span>: <span class="hljs-string">&#x27;icon-[name]&#x27;</span><br>      &#125;)<br>      .<span class="hljs-title function_">end</span>()<br><br>    <span class="hljs-comment">// 目前上不清楚作用</span><br>    config<br>      .<span class="hljs-title function_">when</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">NODE_ENV</span> !== <span class="hljs-string">&#x27;development&#x27;</span>,<br>        <span class="hljs-function"><span class="hljs-params">config</span> =&gt;</span> &#123;<br>          config<br>            .<span class="hljs-title function_">plugin</span>(<span class="hljs-string">&#x27;ScriptExtHtmlWebpackPlugin&#x27;</span>)<br>            .<span class="hljs-title function_">after</span>(<span class="hljs-string">&#x27;html&#x27;</span>)<br>            .<span class="hljs-title function_">use</span>(<span class="hljs-string">&#x27;script-ext-html-webpack-plugin&#x27;</span>, [&#123;<br>            <span class="hljs-comment">// `runtime` must same as runtimeChunk name. default is `runtime`</span><br>              <span class="hljs-attr">inline</span>: <span class="hljs-regexp">/runtime\..*\.js$/</span><br>            &#125;])<br>            .<span class="hljs-title function_">end</span>()<br>          config<br>            .<span class="hljs-property">optimization</span>.<span class="hljs-title function_">splitChunks</span>(&#123;<br>              <span class="hljs-attr">chunks</span>: <span class="hljs-string">&#x27;all&#x27;</span>,<br>              <span class="hljs-attr">cacheGroups</span>: &#123;<br>                <span class="hljs-attr">libs</span>: &#123;<br>                  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;chunk-libs&#x27;</span>,<br>                  <span class="hljs-attr">test</span>: <span class="hljs-regexp">/[\\/]node_modules[\\/]/</span>,<br>                  <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,<br>                  <span class="hljs-attr">chunks</span>: <span class="hljs-string">&#x27;initial&#x27;</span> <span class="hljs-comment">// only package third parties that are initially dependent</span><br>                &#125;,<br>                <span class="hljs-attr">elementUI</span>: &#123;<br>                  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;chunk-elementUI&#x27;</span>, <span class="hljs-comment">// split elementUI into a single package</span><br>                  <span class="hljs-attr">priority</span>: <span class="hljs-number">20</span>, <span class="hljs-comment">// the weight needs to be larger than libs and app or it will be packaged into libs or app</span><br>                  <span class="hljs-attr">test</span>: <span class="hljs-regexp">/[\\/]node_modules[\\/]_?element-ui(.*)/</span> <span class="hljs-comment">// in order to adapt to cnpm</span><br>                &#125;,<br>                <span class="hljs-attr">commons</span>: &#123;<br>                  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;chunk-commons&#x27;</span>,<br>                  <span class="hljs-attr">test</span>: <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;src/components&#x27;</span>), <span class="hljs-comment">// can customize your rules</span><br>                  <span class="hljs-attr">minChunks</span>: <span class="hljs-number">3</span>, <span class="hljs-comment">//  minimum common number</span><br>                  <span class="hljs-attr">priority</span>: <span class="hljs-number">5</span>,<br>                  <span class="hljs-attr">reuseExistingChunk</span>: <span class="hljs-literal">true</span><br>                &#125;<br>              &#125;<br>            &#125;)<br>          <span class="hljs-comment">// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk</span><br>          config.<span class="hljs-property">optimization</span>.<span class="hljs-title function_">runtimeChunk</span>(<span class="hljs-string">&#x27;single&#x27;</span>)<br>        &#125;<br>      )<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="封装svg-icon-组件"><a href="#封装svg-icon-组件" class="headerlink" title="封装svg-icon 组件"></a>封装svg-icon 组件</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;template&gt;<br>  &lt;svg :class=&quot;svgClass&quot; aria-hidden=&quot;true&quot;&gt;<br>    &lt;use :xlink:href=&quot;iconName&quot; /&gt;<br>  &lt;/svg&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>export default &#123;<br>  name: &#x27;SvgIcon&#x27;,<br>  props: &#123;<br>    iconClass: &#123;<br>      type: String,<br>      required: true<br>    &#125;,<br>    className: &#123;<br>      type: String,<br>      default: &#x27;&#x27;<br>    &#125;<br>  &#125;,<br>  computed: &#123;<br>    iconName() &#123;<br>      return `#icon-$&#123;this.iconClass&#125;`<br>    &#125;,<br>    svgClass() &#123;<br>      if (this.className) &#123;<br>        return &#x27;svg-icon &#x27; + this.className<br>      &#125; else &#123;<br>        return &#x27;svg-icon&#x27;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br>&lt;/script&gt;<br><br></code></pre></td></tr></table></figure>

<h3 id="全局挂载"><a href="#全局挂载" class="headerlink" title="全局挂载"></a>全局挂载</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">SvgIcon</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@/components/SvgIcon/index.vue&#x27;</span><br><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">component</span>(<span class="hljs-string">&#x27;svg-icon&#x27;</span>, <span class="hljs-title class_">SvgIcon</span>)<br><br><span class="hljs-keyword">const</span> req = <span class="hljs-built_in">require</span>.<span class="hljs-title function_">context</span>(<span class="hljs-string">&#x27;./svg&#x27;</span>, <span class="hljs-literal">false</span>, <span class="hljs-regexp">/\.svg$/</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-title function_">requireAll</span> = requireContext =&gt; requireContext.<span class="hljs-title function_">keys</span>().<span class="hljs-title function_">map</span>(requireContext)<br>requireAll(req)<br></code></pre></td></tr></table></figure>



              
            </div>
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="category-chain-item">学习笔记</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/vue/">#vue</a>
      
        <a href="/tags/svg/">#svg</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue中使用svg图标</div>
      <div>https://zjy365.github.io/2021/10/20/vue中使用svg图标/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>zjy</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年10月20日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/10/25/css%E5%8A%A8%E7%94%BB/" title="CSS动画">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">CSS动画</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/12/element%20%E5%AF%BC%E8%88%AA%E8%8F%9C%E5%8D%95%E6%8A%98%E5%8F%A0%E5%8A%A8%E7%94%BB%EF%BC%8C%E6%96%87%E5%AD%97%E4%B8%8D%E9%9A%90%E8%97%8F/" title="element 导航菜单折叠动画，文字不隐藏">
                        <span class="hidden-mobile">element 导航菜单折叠动画，文字不隐藏</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      豫ICP备2021008845号-1
    </a>
  </span>
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>






  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script>
  (function() {
    var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
    var enableCopy = CONFIG.copy_btn;
    if (!enableLang && !enableCopy) {
      return;
    }

    function getBgClass(ele) {
      return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
    }

    var copyTmpl = '';
    copyTmpl += '<div class="code-widget">';
    copyTmpl += 'LANG';
    copyTmpl += '</div>';
    jQuery('.markdown-body pre').each(function() {
      var $pre = jQuery(this);
      if ($pre.find('code.mermaid').length > 0) {
        return;
      }
      if ($pre.find('span.line').length > 0) {
        return;
      }

      var lang = '';

      if (enableLang) {
        lang = CONFIG.code_language.default;
        if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
          lang = $pre[0].children[0].classList[1];
        } else if ($pre[0].getAttribute('data-language')) {
          lang = $pre[0].getAttribute('data-language');
        } else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
          lang = $pre[0].children[0].classList[1];
          $pre.parent().addClass('code-wrapper');
        } else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
          $pre.wrap('<div class="code-wrapper"></div>');
        }
        lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
      }
      $pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
        getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));

      if (enableCopy) {
        Fluid.utils.createScript('https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js', function() {
          var clipboard = new window.ClipboardJS('.copy-btn', {
            target: function(trigger) {
              var nodes = trigger.parentNode.childNodes;
              for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].tagName === 'CODE') {
                  return nodes[i];
                }
              }
            }
          });
          clipboard.on('success', function(e) {
            e.clearSelection();
            e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
            setTimeout(function() {
              e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
            }, 2000);
          });
        });
      }
    });
  })();
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
